package com.ruoyi.system.controller;

import java.util.*;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.ZhuanjiaRenkouhecha;
import com.ruoyi.system.domain.ZhuanjiaRenkouhechaPici;
import com.ruoyi.system.service.IZhuanjiaRenkouhechaPiciService;
import com.ruoyi.system.service.IZhuanjiaRenkouhechaService;
import com.ruoyi.web.controller.tool.JsonResult;
import com.ruoyi.web.controller.tool.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Zhuanjia;
import com.ruoyi.system.service.IZhuanjiaService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 专家Controller
 * 
 * @author dy
 * @date 2024-05-29
 */
@Controller
@RequestMapping("/system/zhuanjia")
public class ZhuanjiaController extends BaseController
{
    private String prefix = "system/zhuanjia";

    @Autowired
    private IZhuanjiaService zhuanjiaService;

    @Autowired
    private IZhuanjiaRenkouhechaService zhuanjiaRenkouhechaService;

    @Autowired
    private IZhuanjiaRenkouhechaPiciService zhuanjiaRenkouhechaPiciService;

    @RequiresPermissions("system:zhuanjia:view")
    @GetMapping()
    public String zhuanjia()
    {
        return prefix + "/zhuanjia";
    }

    /**
     * 查询专家列表
     */
    @RequiresPermissions("system:zhuanjia:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Zhuanjia zhuanjia)
    {
        startPage();
        List<Zhuanjia> list = zhuanjiaService.selectZhuanjiaList(zhuanjia);
        return getDataTable(list);
    }

    /**
     * 导出专家列表
     */
    @RequiresPermissions("system:zhuanjia:export")
    @Log(title = "专家", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Zhuanjia zhuanjia)
    {
        List<Zhuanjia> list = zhuanjiaService.selectZhuanjiaList(zhuanjia);
        ExcelUtil<Zhuanjia> util = new ExcelUtil<Zhuanjia>(Zhuanjia.class);
        return util.exportExcel(list, "专家数据");
    }

    /**
     * 新增专家
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存专家
     */
    @RequiresPermissions("system:zhuanjia:add")
    @Log(title = "专家", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Zhuanjia zhuanjia)
    {
        return toAjax(zhuanjiaService.insertZhuanjia(zhuanjia));
    }

    /**
     * 修改专家
     */
    @RequiresPermissions("system:zhuanjia:edit")
    @GetMapping("/edit/{zhuanjiaid}")
    public String edit(@PathVariable("zhuanjiaid") String zhuanjiaid, ModelMap mmap)
    {
        Zhuanjia zhuanjia = zhuanjiaService.selectZhuanjiaByZhuanjiaid(zhuanjiaid);
        mmap.put("zhuanjia", zhuanjia);
        return prefix + "/edit";
    }

    /**
     * 修改保存专家
     */
//    @RequiresPermissions("system:zhuanjia:edit")
    @Log(title = "专家", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Zhuanjia zhuanjia)
    {
        return toAjax(zhuanjiaService.updateZhuanjia(zhuanjia));
    }

    /**
     * 修改保存专家
     */
//    @RequiresPermissions("system:zhuanjia:edit")
    @Log(title = "专家", businessType = BusinessType.UPDATE)
    @PostMapping("/hechajianzai")//核查专家 健在
    @ResponseBody
    public JsonResult<?> hechajianzai(String biandongid,Zhuanjia zhuanjia)
    {
        //处理人口核查
        ZhuanjiaRenkouhecha mmhechatemp = new ZhuanjiaRenkouhecha();
        mmhechatemp.setBiandongid(biandongid);
        List<ZhuanjiaRenkouhecha> mmTemplist = zhuanjiaRenkouhechaService.selectZhuanjiaRenkouhechaList(mmhechatemp);
        if (!mmTemplist.isEmpty()){
            if (mmTemplist.size()>1){
                return JsonResult.getErrorResult("当前专家状态关联人口核查数据有误");
            }
            if (mmTemplist.size()==1){
                ZhuanjiaRenkouhecha mmHecha = mmTemplist.get(0);
                mmHecha.setChuliriqi(new Date());
                mmHecha.setShifouchuli(1l);
                mmHecha.setChulijieguo("核查专家健在");
                zhuanjiaRenkouhechaService.updateZhuanjiaRenkouhecha(mmHecha);

                String piciid = mmHecha.getPiciid();
                ZhuanjiaRenkouhechaPici mmPici = zhuanjiaRenkouhechaPiciService.selectZhuanjiaRenkouhechaPiciById(piciid);
                mmPici.setWeihecha(mmPici.getWeihecha()-1);
                mmPici.setYihecha(mmPici.getYihecha()+1);
                zhuanjiaRenkouhechaPiciService.updateZhuanjiaRenkouhechaPici(mmPici);
            }
        }
        return zhuanjiaService.updateZhuanjia(zhuanjia)==1?JsonResult.getSuccessResult("提交成功"):JsonResult.getErrorResult("提交失败");
    }


    /**
     * 删除专家
     */
    @RequiresPermissions("system:zhuanjia:remove")
    @Log(title = "专家", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zhuanjiaService.deleteZhuanjiaByZhuanjiaids(ids));
    }

    /**
     * 专家管理-查询统计
     */
    @PostMapping("/customlist")
    @ResponseBody
    public JsonResult<?> customlist(String deptid,
                                          String xingming,
                                          String deptname,
                                          String deptlevel,
                                          String gongzuodanwei,
                                          String ruxuannianfen,
                                          String zhuanjiazhuangtai,
                                          String zhuanjiazhuangtaibiandongshijian_kaishi,
                                          String zhuanjiazhuangtaibiandongshijian_jieshu,
                                          int pageindex, int pagesize)
    {
        try {
            //获取当前用户
            Subject subject = ShiroUtils.getSubject();
            int mmCount = 0;

            List<Map<String, Object>> list = new ArrayList<>();
            //专家中心超管、专家中心普通管理员、单位超管查本单位和所有下属单位
            if (subject.hasRole("zhongxin_commonadmin")||subject.hasRole("zhongxin_superadmin")||subject.hasRole("dept_superadmin"))
            {
                list = zhuanjiaService.chaxuntongjilist(deptid,xingming,deptname,deptlevel,gongzuodanwei,ruxuannianfen, zhuanjiazhuangtai,zhuanjiazhuangtaibiandongshijian_kaishi, zhuanjiazhuangtaibiandongshijian_jieshu, pageindex, pagesize);
                mmCount = zhuanjiaService.chaxuntongjilistCount(deptid,xingming,deptname,deptlevel,gongzuodanwei,ruxuannianfen, zhuanjiazhuangtai,zhuanjiazhuangtaibiandongshijian_kaishi, zhuanjiazhuangtaibiandongshijian_jieshu);
            }
            //单位普通管理员查本单位
            if (subject.hasRole("dept_commonadmin"))
            {
                list = zhuanjiaService.chaxuntongjilistCommon(deptid,xingming,deptname,deptlevel,gongzuodanwei,ruxuannianfen, zhuanjiazhuangtai, zhuanjiazhuangtaibiandongshijian_kaishi, zhuanjiazhuangtaibiandongshijian_jieshu, pageindex, pagesize);
                mmCount = zhuanjiaService.chaxuntongjilistCommonCount(deptid,xingming,deptname,deptlevel,gongzuodanwei,ruxuannianfen, zhuanjiazhuangtai, zhuanjiazhuangtaibiandongshijian_kaishi, zhuanjiazhuangtaibiandongshijian_jieshu);
            }

            Map<String, Integer> count =new HashMap<>();

            //专家中心超管、单位超管查询 正常状态专家###名，其中1990年###名、1991年###名、1992年###名、1993年###名、1994年###名
            if (subject.hasRole("zhongxin_superadmin")||subject.hasRole("dept_superadmin"))
            {
                count = zhuanjiaService.Countchaxuntongji(deptid,xingming,deptname,deptlevel,gongzuodanwei,ruxuannianfen, zhuanjiazhuangtai, zhuanjiazhuangtaibiandongshijian_kaishi, zhuanjiazhuangtaibiandongshijian_jieshu);
            }
            //专家中心普通管理员、单位普通管理员查询 正常状态专家###名，其中1990年###名、1991年###名、1992年###名、1993年###名、1994年###名
            if (subject.hasRole("dept_commonadmin")||subject.hasRole("zhongxin_commonadmin"))
            {
                count = zhuanjiaService.Countchaxuntongjicommon(deptid,xingming,deptname,deptlevel,gongzuodanwei,ruxuannianfen, zhuanjiazhuangtai, zhuanjiazhuangtaibiandongshijian_kaishi, zhuanjiazhuangtaibiandongshijian_jieshu);
            }


            PageInfo mmPageInfo = new PageInfo(pageindex, pagesize, mmCount);

            Map<String,Object> mmMap = new HashMap<>();
            mmMap.put("zhuanjialist",list);
            mmMap.put("PageInfo", mmPageInfo);
            mmMap.put("CountInfo", count);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            logger.error("zhuanjia -> customlist: "+e.getMessage());
            return JsonResult.getErrorResult("查询专家管理查询统计列表失败");
        }
    }
}
